package com.designoutput.ui.options.components
{
	import com.designoutput.enum.TextHAlign;
	import com.designoutput.enum.ToolType;
	import com.designoutput.icon.TextCenterIcon;
	import com.designoutput.icon.TextJustifyIcon;
	import com.designoutput.icon.TextLeftIcon;
	import com.designoutput.icon.TextRightIcon;
	import com.designoutput.ui.controls.ToolButton;
	import com.designoutput.ui.options.TextOption;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class TextHAlignGroup extends Sprite
	{
		private var baseOption:TextOption;
		private var left:ToolButton;
		private var center:ToolButton;
		private var right:ToolButton;
		private var justify:ToolButton;
		internal var at:ToolButton;
		
		private var _textHAlign:String = TextHAlign.LEFT;
		
		public function TextHAlignGroup(baseOption:TextOption)
		{
			this.baseOption = baseOption;
			this.build();
		}

		private function build():void{
			
			left = new ToolButton(ToolType.TextLeft, new TextLeftIcon(0,0),5,5);
			left.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			left.x = 0;
			left.y = 10;
			this.addChild(left);
			
			center = new ToolButton(ToolType.TextCenter, new TextCenterIcon(0,0),5,5);
			center.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			center.x = 30;
			center.y = 10;
			this.addChild(center);
			
			right = new ToolButton(ToolType.TextRight, new TextRightIcon(0,0),5,5);
			right.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			right.x = 60;
			right.y = 10;
			this.addChild(right);
			
			justify = new ToolButton(ToolType.TextJustify, new TextJustifyIcon(0, 0), 5, 5);
			justify.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			justify.x = 90;
			justify.y = 10;
			this.addChild(justify);
		}
		
		public function get textHAlign():String
		{
			return _textHAlign;
		}
		
		public function set textHAlign(value:String):void
		{
			_textHAlign = value;
			setHAlign(value);
		}
		
		private function setHAlign(value:String):void{
			if (this.at != null)
			{
				this.at.Mark(false);
			}
			switch(value){
				case TextHAlign.CENTER:
					this.at = center;
					break;
				case TextHAlign.RIGHT:
					this.at = right;
					break;
				case TextHAlign.LEFT:
					this.at = left;
					break;
				case TextHAlign.JUSTIFY:
					this.at = justify;
					break;
			}
			this.at.Mark(true);
		}
		
		private function ToolMouseDown(evt:MouseEvent):void{
			if (this.at != null)
			{
				this.at.Mark(false);
			}
			this.at=evt.currentTarget as ToolButton;
			switch(this.at.type){
				case ToolType.TextCenter:
					this.baseOption.align = TextHAlign.CENTER;
					break;
				case ToolType.TextRight:
					this.baseOption.align = TextHAlign.RIGHT;
					break;
				case ToolType.TextLeft:
					this.baseOption.align = TextHAlign.LEFT;
					break;
				case ToolType.TextJustify:
					this.baseOption.align = TextHAlign.JUSTIFY;
					break;
			}
			this.baseOption.updateTextElemet();
			this.at.Mark(true);
		}
	}
}